package chapter21;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @program: exercise
 * @description: 统计关键字
 * @author: 陈庆彪
 * @create: 2020-02-24 09:33
 **/
public class CountKeywords{
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input =new Scanner(System.in);
        System.out.println("Enter a java source file: ");
        String filename=input.nextLine();
        File file=new File(filename);
        if (file.exists()){
            System.out.println("the number of keywords in "+filename+" is"+countKeywords(file));
        }else {
            System.out.println("file"+filename+"does not exists");
        }
    }

    private static int countKeywords(File file) throws FileNotFoundException {
        String [] keywordString={"abstract","assert","boolean","break","byte","case","catch","char","class","const","continue","default"
        ,"do","double","else","enum","extends","for","final","finally","float","goto","if","implements","import","instanceof",
                "int","interface","long","native","new","package","private","protected","public","return","short","static","strictfp","super",
                "switch","synchronized","this","throw","throws","transient","try","void","volatile","while","true","false","null"
        };
        Set<String> keywordSet=new HashSet<>(Arrays.asList(keywordString));
        int count=0;
        Scanner input = new Scanner(file);
        while (input.hasNext()){
            String word=input.next();
            count++;
        }
        return count;
    }

}
